package com.bdth.bdthiss.util;

import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Administrator on 2018/11/27.
 */
public class FileUtils {
    /**
     * 创建文件夹
     * @param path
     */
    public static void createDir(String path){
        File dir=new File(path);
        if(!dir.exists())
            dir.mkdir();
    }

    /**
     * 创建新文件
     * @param path
     * @param filename
     * @throws IOException
     */
    public static void createFile(String path,String filename) throws IOException {
        File file=new File(path+"/"+filename);
        if(!file.exists())
            file.createNewFile();
    }

    /**
     * 删除文件
     * @param path
     * @param filename
     */
    public static void delFile(String path,String filename) {
        File file=new File(path+"/"+filename);
        if(file.exists()&&file.isFile())
            file.delete();
    }

    /**
     * 删除文件夹
     * @param path
     */
    public static void delDir(String path) {
        File dir=new File(path);
        if(dir.exists()) {
            File[] tmp=dir.listFiles();
            for(int i=0;i<tmp.length;i++) {
                if(tmp[i].isDirectory()) {
                    delDir(path+"/"+tmp[i].getName());
                }
                else {
                    tmp[i].delete();
                }
            }
            dir.delete();
        }
    }

    /**
     * 读文件
     * @param path
     * @return
     * @throws IOException
     */
    public static String FileInputStreamDemo(String path) throws IOException {
        File file=new File(path);
        if(!file.exists()||file.isDirectory())
            throw new FileNotFoundException();
        FileInputStream fis=new FileInputStream(file);
        byte[] buf = new byte[1024];
        StringBuffer sb=new StringBuffer();
        while((fis.read(buf))!=-1) {
            sb.append(new String(buf));
            buf=new byte[1024];//重新生成，避免和上次读取的数据重复
        }
        return sb.toString();
    }

    /**
     * Function测试
     */
    public static void functionTest() {
        Function<Integer, Integer> f = s -> s++;
        Function<Integer, Integer> g = s -> s * 2;

        /**
         * 下面表示在执行F时，先执行G，并且执行F时使用G的输出当作输入。
         * 相当于以下代码：
         * Integer a = g.apply(1);
         * System.out.println(f.apply(a));
         */
        System.out.println(f.compose(g).apply(1));

        /**
         * 表示执行F的Apply后使用其返回的值当作输入再执行G的Apply；
         * 相当于以下代码
         * Integer a = f.apply(1);
         * System.out.println(g.apply(a));
         */
        System.out.println(f.andThen(g).andThen(g).andThen(g).apply(1));

        /**
         * identity方法会返回一个不进行任何处理的Function，即输出与输入值相等；
         */
        System.out.println(Function.identity().apply("a"));
    }

    /**
     * 获取一个文件的md5值(可处理大文件)
     * @return md5 value
     */
    public static String getMD5(File file) {
        FileInputStream fileInputStream = null;
        try {
            MessageDigest MD5 = MessageDigest.getInstance("MD5");
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                MD5.update(buffer, 0, length);
            }
            return new String(Hex.encodeHex(MD5.digest()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fileInputStream != null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception{
        //functionTest();
//        List<Double>  l = new ArrayList<>();
//        Stream.generate(() -> Math.random()).limit(10).forEach(l::add);
//        Stream<Double>  random = Stream.generate(() -> Math.random()).limit(10);
//        List<Double> list = random.collect(Collectors.toList());
//        l.forEach(System.out::println);
        File file = new File("C:\\20191225110200515.png");
        String md5 = getMD5(file);
        System.out.println(md5);
    }

}
